//
//  HYDoubleSlideView.m
//  LEVE
//
//  Created by 上官惠阳 on BtnWidth17/3/3.
//  Copyright © BtnWidth17年 dashuju. All rights reserved.
//

#import "HYDoubleSlideView.h"

#define BtnWidth 25.f
@interface HYDoubleSlideView ()
@property (nonatomic,assign)CGFloat CurrentminValue;
@property (nonatomic,assign)CGFloat CurrentMaxValue;

@end
@implementation HYDoubleSlideView
{
    UIView *_minSliderBtnLine;
    UIView *_maxSliderBtnLine;
    UIView *_mainSliderLine;


    CGFloat _constOffY;

    CGFloat _tatol;
    CGFloat _maxWidth;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self == [super initWithFrame:frame]) {
        _maxWidth = BtnWidth;
        [self createMainView];
    }
    return self;
}
- (void)createMainView
{
    self.minValue = 0.0;
    self.maxValue = 0.0;

    _mainSliderLine = [[UIView alloc]initWithFrame:CGRectMake(BtnWidth/2,self.height/2 - 2/2, self.width-BtnWidth, 2)];
    _mainSliderLine.backgroundColor = [UIColor darkGrayColor];
    [self addSubview:_mainSliderLine];

    _minSliderBtnLine = [[UIView alloc]initWithFrame:CGRectMake(BtnWidth/2, _mainSliderLine.top, 0, _mainSliderLine.height)];
    _minSliderBtnLine.backgroundColor = [UIColor redColor];
    [self addSubview:_minSliderBtnLine];

    _maxSliderBtnLine = [[UIView alloc]initWithFrame:CGRectMake(self.width-BtnWidth/2, _mainSliderLine.top, 0, _mainSliderLine.height)];
    _maxSliderBtnLine.backgroundColor = [UIColor redColor];
    [self addSubview:_maxSliderBtnLine];

    UIButton *minSliderButton = [[UIButton alloc]initWithFrame:CGRectMake(0,(self.height - BtnWidth)/2, BtnWidth, BtnWidth)];
    minSliderButton.backgroundColor = [UIColor whiteColor];
    minSliderButton.showsTouchWhenHighlighted = YES;
    minSliderButton.layer.cornerRadius = minSliderButton.width/2.0f;
    minSliderButton.layer.masksToBounds = YES;
    minSliderButton.layer.borderColor = KGrayColor.CGColor;
    minSliderButton.layer.borderWidth = 0.5;
    UIPanGestureRecognizer *minSliderButtonPanGestureRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panMinSliderButton:)];
    [minSliderButton addGestureRecognizer:minSliderButtonPanGestureRecognizer];
    [self addSubview:minSliderButton];
    _minSliderBtn = minSliderButton;


    UIButton *maxSliderButton = [[UIButton alloc]initWithFrame:CGRectMake(self.width - _maxWidth, (self.height - BtnWidth)/2, _maxWidth, BtnWidth)];
    maxSliderButton.backgroundColor = [UIColor whiteColor];
    maxSliderButton.showsTouchWhenHighlighted = YES;
    maxSliderButton.layer.cornerRadius = minSliderButton.width/2.0f;
    maxSliderButton.layer.masksToBounds = YES;
    maxSliderButton.layer.borderColor = KGrayColor.CGColor;
    maxSliderButton.layer.borderWidth = 0.5;
    UIPanGestureRecognizer *maxSliderButtonPanGestureRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panMaxSliderButton:)];
    [maxSliderButton addGestureRecognizer:maxSliderButtonPanGestureRecognizer];
    [self addSubview:maxSliderButton];
    _maxSliderBtn = maxSliderButton;
    _constOffY = _minSliderBtn.centerY;
}

- (void)panMinSliderButton:(UIPanGestureRecognizer *)pgr
{
    CGPoint point = [pgr translationInView:self];
    static CGPoint center;
    if (pgr.state == UIGestureRecognizerStateBegan) {
        center = pgr.view.center;
    }
    pgr.view.center = CGPointMake(center.x + point.x, center.y + point.y);
    pgr.view.centerY = _constOffY;


    if (pgr.view.right > _maxSliderBtn.left) {
        pgr.view.right = _maxSliderBtn.left;
    }else{
        if (pgr.view.centerX < BtnWidth/2) {
            pgr.view.centerX = BtnWidth/2;
        }
        if (pgr.view.centerX > self.width-BtnWidth/2) {
            pgr.view.centerX = self.width-BtnWidth/2;
        }
    }
    _minSliderBtnLine.frame = CGRectMake(_minSliderBtnLine.left, _minSliderBtnLine.top,  pgr.view.centerX-_minSliderBtnLine.left, _minSliderBtnLine.height);

    [self valueMinChange:pgr.view.right];

    if (_changeValueBlock) {
        _changeValueBlock(_currentMinValue,_currentMaxValue);
    }
}

- (void)panMaxSliderButton:(UIPanGestureRecognizer *)pgr
{
    CGPoint point = [pgr translationInView:self];
    static CGPoint center;
    if (pgr.state == UIGestureRecognizerStateBegan) {
        center = pgr.view.center;
    }
    pgr.view.center = CGPointMake(center.x + point.x, center.y + point.y);
    pgr.view.centerY = _constOffY;

    if (pgr.view.left < _minSliderBtn.right) {
        pgr.view.left = _minSliderBtn.right;
    }else{
        if (pgr.view.centerX < BtnWidth/2) {
            pgr.view.centerX = BtnWidth/2;
        }
        if (pgr.view.centerX > self.width-BtnWidth/2) {
            pgr.view.centerX = self.width-BtnWidth/2;
        }
    }
    _maxSliderBtnLine.frame = CGRectMake(pgr.view.centerX, _maxSliderBtnLine.top, self.width-pgr.view.centerX-BtnWidth/2, _maxSliderBtnLine.height);
    [self valueMaxChange:pgr.view.left];

    if (_changeValueBlock) {
        _changeValueBlock(_currentMinValue,_currentMaxValue);
    }
}

- (void)valueMinChange:(CGFloat)num
{
    if (_isDouble) {
        _currentMinValue = _minValue + (_tatol * (num-BtnWidth));
    }else{
        CGFloat total = (_maxValue - _minValue)/(self.width - BtnWidth);
        _currentMinValue = _minValue + (total * (num - BtnWidth));
    }
}
- (void)valueMaxChange:(CGFloat)num
{
    _currentMaxValue = _minValue +(_tatol * (num-BtnWidth));
}
-(void)setMinValue:(CGFloat)minValue
{
    _minValue = minValue;
    _tatol = (_maxValue - _minValue)/(self.width - 2*BtnWidth);
    _currentMinValue = minValue;
}

-(void)setMaxValue:(CGFloat)maxValue
{
    _maxValue = maxValue;
    _tatol = (_maxValue - _minValue)/(self.width - 2*BtnWidth);
    _currentMaxValue = maxValue;
}

- (void)setCurrentMinValue:(CGFloat)currentMinValue
{
    if (currentMinValue > _maxValue) {
        return;
    }
    _currentMinValue = currentMinValue;
    if (_isDouble) {
        _minSliderBtn.right = (_currentMinValue- _minValue) / _tatol + BtnWidth;
    }else{
        CGFloat total = (_maxValue - _minValue)/(self.width - BtnWidth);
        _minSliderBtn.right = (_currentMinValue- _minValue) / total + BtnWidth;
    }
    _minSliderBtnLine.frame = CGRectMake(_minSliderBtnLine.left, _minSliderBtnLine.top,  _minSliderBtn.centerX-_minSliderBtnLine.left, _minSliderBtnLine.height);
}
- (void)setCurrentMaxValue:(CGFloat)currentMaxValue
{
    if (currentMaxValue < _minValue) {
        return;
    }
    if (currentMaxValue < _CurrentminValue) {
        return;
    }
    _currentMaxValue = currentMaxValue;
    _maxSliderBtn.left = (_currentMaxValue- _minValue) / _tatol + BtnWidth;

    _maxSliderBtnLine.frame = CGRectMake(_maxSliderBtn.centerX, _maxSliderBtnLine.top, self.width - _maxSliderBtn.centerX-BtnWidth/2, _maxSliderBtnLine.height);
}
-(void)setMinTintColor:(UIColor *)minTintColor
{
    _minTintColor = minTintColor;
    _minSliderBtnLine.backgroundColor = minTintColor;
}

-(void)setMaxTintColor:(UIColor *)maxTintColor
{
    _maxTintColor = maxTintColor;
    _maxSliderBtnLine.backgroundColor = maxTintColor;
}

-(void)setMainTintColor:(UIColor *)mainTintColor
{
    _mainTintColor = mainTintColor;
    _mainSliderLine.backgroundColor = mainTintColor;
}
- (void)setIsDouble:(BOOL)isDouble
{
    _isDouble = isDouble;
    _maxSliderBtn.hidden = !isDouble;

    _maxWidth = isDouble ? BtnWidth : 0;
    _maxSliderBtn.left = self.width - _maxWidth;
}
@end
